﻿#include "logger.h"

namespace breaking
{
	logger::logger()
	{
		m_priority = log_priority::LOG_DEBUG;
	}

	logger::~logger()
	{
		m_mutex.lock();
		for (auto& appender : m_appenders)
		{
			appender->close();
			delete appender;
		}
		m_appenders.clear();
		m_mutex.unlock();
	}

	bool logger::add_appender(log_appender* appender)
	{
		if (appender == nullptr || !appender->open())
		{
			delete appender;
			return false;
		}

		m_mutex.lock();
		m_appenders.push_back(appender);
		m_mutex.unlock();
		return true;
	}

	void logger::set_priority(log_priority priority)
	{
		m_priority = priority;
	}

	bool logger::is_loggable(log_priority priority)
	{
		return priority >= m_priority;
	}

	void logger::add_log_event(const log_event& event)
	{
		m_mutex.lock();
		for (auto appender : m_appenders)
		{
			appender->append(event);
		}
		m_mutex.unlock();
	}
}